package com.ligoudan;

import com.ligoudan.config.ConfigHelper;
import com.ligoudan.converter.Converter;
import com.ligoudan.core.GenContext;
import com.ligoudan.model.GenImageModel;
import com.ligoudan.ui.ApplicationFrame;
import com.formdev.flatlaf.FlatLightLaf;

import javax.swing.SwingUtilities;
import java.util.LinkedList;
import java.util.Properties;
import java.util.logging.Logger;

public class Launcher {

    private static final Logger LOGGER = Logger.getLogger(Launcher.class.getName());

    public static void main(String[] args) {
        GenContext context = buildContext();
        SwingUtilities.invokeLater(() -> {
            try {
                // UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
                // System.setProperty("flatlaf.useWindowDecorations", "false");
                FlatLightLaf.setup();
                LOGGER.info("Java Home: " + System.getProperty("java.home"));
                LOGGER.info("Java Version: " + System.getProperty("java.version"));
            } catch (Exception e) {
                LOGGER.warning(e.getMessage());
            }
            launch(context);
        });
    }

    public static void launch(GenContext context) {
        new ApplicationFrame(context).start();
    }

    private static GenContext buildContext() {
        GenContext context = new GenContext();

        ConfigHelper configHelper = new ConfigHelper();
        Properties genConfig = configHelper.loadConfig();
        GenImageModel genModel = Converter.config2Model(genConfig);

        context.setConfigHelper(configHelper);
        context.setGenImageConfig(genConfig);
        context.setGenImageModel(genModel);
        context.setFormComponents(new LinkedList<>());

        configHelper.storageConfig(genModel);
        return context;
    }

}
